﻿namespace Microsoft.ExternalResearch.CreativeCommons.Word
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Microsoft.ExternalResearch.CreativeCommons.Common;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Represents the Creative Commons Add-in for Microsoft Word.
    /// </summary>
    public partial class ThisAddIn
    {
        /// <summary>
        /// Represents the Word application object.
        /// </summary>
        private static Word.Application wordApplication;


        /// <summary>
        /// Raised when the application starts.
        /// </summary>
        public static event EventHandler ApplicationStartedEvent;

        /// <summary>
        /// Gets or sets the Word application object.
        /// </summary>
        public static Word.Application WordApplication
        {
            get
            {
                return wordApplication;
            }

            set
            {
                wordApplication = value;
            }
        }

        /// <summary>
        /// Raises the ApplicationStarted event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void ThisAddInStartup(object sender, System.EventArgs e)
        {
            ThisAddIn.WordApplication = this.Application;

            if (null != ApplicationStartedEvent)
            {
                ApplicationStartedEvent(this, null);
            }
        }

        /// <summary>
        /// Gets a value indicating whether a document is opened.
        /// </summary>
        public static bool HasDocument()
        {
            bool hasDocument = false;
            try
            {
                if (null != WordApplication.ActiveDocument)
                {
                    hasDocument = true;
                }
            }
            catch (Exception)
            {
            }

            return hasDocument;
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddInStartup);
        }

        #endregion
    }
}
